<?php

class m_dokumen extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					proyek_nama,
					dokumen_nama,
					dokumen_url,
					dokumen_asli,
					dokumen_posisi,
					t_proyek_id,
					dokumen_id
				FROM t_dokumen
				LEFT JOIN t_proyek ON t_proyek_id = proyek_id
				WHERE 
					dokumen_sys_active = '1'
		";
		
		if (@$dokumen_nama != "") $sql .= " AND dokumen_nama like '%".@$dokumen_nama."%'";
		if (@$dokumen_url != "") $sql .= " AND dokumen_url like '%".@$dokumen_url."%'";
		if (@$dokumen_asli != "") $sql .= " AND if(dokumen_asli = 1, 'asli' , 'copy')  like '%".@$dokumen_asli."%'";
		if (@$dokumen_posisi != "") $sql .= " AND dokumen_posisi like '%".@$dokumen_posisi."%'";
		if (@$t_proyek_id != "") $sql .= " AND t_proyek_id like '%".@$t_proyek_id."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('t_dokumen',$data);
		return 1;
	}
	
	function _update($data,$id){
		$this->db->where('dokumen_id',$id);
		$this->db->update('t_dokumen',$data);
		return 1;
	}
	
	function _delete($data,$id){
		$this->db->where('dokumen_id',$id);
		$this->db->update('t_dokumen',$data);
		return 1;
	}
	
	/* another function */
	
	function getDataProyek(){
		$sql = "SELECT * FROM t_proyek";
		$query = $this->db->query($sql);
		$data = array("");
		foreach($query->result() as $row){
			$data[$row->proyek_id] = $row->proyek_nama;
		}
		return $data;
	}
}